package zbu.eb.dao;
import zbu.eb.db.ConnectionMS;
import zbu.eb.model.EBike;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class EBikeDaoImpl implements EBikeDao {
    public Connection conn;//链接
    public PreparedStatement psmt;//预处理
    public ResultSet rs;//查询结果集
    public String sql;//sql语句

    @Override
    public void addBike() throws SQLException, ClassNotFoundException {
        System.out.println("添加车辆...");
        //插入
        sql = "insert into electricbike values(?,?,?,?,?)";
        //查找数据库中的数据
        String sqlFind = "select * from electricbike";
        conn = ConnectionMS.getConnection();
        psmt = conn.prepareStatement(sqlFind);
        rs = psmt.executeQuery();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入电动车名：");
        String addBikeName = sc.nextLine();
        //根据电动车名查找是否有重复的，有重复的则禁止添加
        while (rs.next()) {
            if (addBikeName.equals(rs.getString("电动车名"))) {
                System.out.println("已经有这种车了，添加失败！！！");
                conn.close();
                psmt.close();
                rs.close();
                return;
            }
        }
        psmt = conn.prepareStatement(sql);
        psmt.setString(1,addBikeName);
        System.out.println("请输入产地：");
        psmt.setString(2,sc.nextLine());
        System.out.println("请输入电动车类型：");
        psmt.setString(3,sc.nextLine());
        System.out.println("请输入电动车售价：");
        psmt.setInt(4,sc.nextInt());
        System.out.println("请输入电动车数量：");
        psmt.setInt(5,sc.nextInt());
        psmt.executeUpdate();
        System.out.println("添加成功！！！");
        conn.close();
        psmt.close();
        rs.close();
    }

    @Override
    public void delBike() throws SQLException, ClassNotFoundException {
        System.out.println("删除车辆...");
        //删除
        sql = "delete from electricbike where 电动车名=?";
        //查找数据库中的数据
        String sqlFind = "select * from electricbike";
        conn = ConnectionMS.getConnection();
        psmt = conn.prepareStatement(sqlFind);
        rs = psmt.executeQuery();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入电动车名：");
        String delBikeName = sc.nextLine();
        //根据电动车名查找
        while (rs.next()) {
            if (delBikeName.equals(rs.getString("电动车名"))) {
                psmt = conn.prepareStatement(sql);
                psmt.setString(1,delBikeName);
                psmt.executeUpdate();
                System.out.println("删除成功！！！");
                conn.close();
                psmt.close();
                rs.close();
                return;
            }
        }
        System.out.println("没有这种车，删除失败！！！");
        conn.close();
        psmt.close();
        rs.close();
    }

    @Override
    public void findBike() throws SQLException, ClassNotFoundException {
        System.out.println("查找车辆...");
        sql = "select * from electricbike where 电动车名=?";
        System.out.println("请输入要查找的电动车名：");
        Scanner sc = new Scanner(System.in);
        String findName = sc.nextLine();
        conn = ConnectionMS.getConnection();
        psmt = conn.prepareStatement(sql);
        psmt.setString(1,findName);
        rs = psmt.executeQuery();
        int flag = 0;//判断库中是否有数据的条件
        while (rs.next()) {
            String bikeName = rs.getString("电动车名");
            String source = rs.getString("产地");
            String type = rs.getString("电动车类型");
            int price = rs.getInt("售价");
            int count = rs.getInt("数量");
            System.out.println("EBike{" +
                    "电动车名='" + bikeName + '\'' +
                    ", 产地='" + source + '\'' +
                    ", 类型='" + type + '\'' +
                    ", 售价=" + price +
                    ", 数量=" + count +
                    '}');
            flag = 1;
        }
        if (flag == 0) {
            System.out.println("没有你找的这种辆车！！！");
        }
        rs.close();
        psmt.close();
        conn.close();
    }

    @Override
    public void alterBike() throws SQLException, ClassNotFoundException {
        System.out.println("修改车辆...");
        System.out.println("请输入你要修改的车辆名称：");
        Scanner sc = new Scanner(System.in);
        String alterBikeName = sc.nextLine();
        //查找数据库中的数据
        String sqlFind = "select * from electricbike";
        conn = ConnectionMS.getConnection();
        psmt = conn.prepareStatement(sqlFind);
        rs = psmt.executeQuery();

        while (rs.next()) {
            if (alterBikeName.equals(rs.getString("电动车名"))) {
                while (true) {
                    System.out.println("修改菜单：");
                    System.out.println("1.修改车名");
                    System.out.println("2.修改产地");
                    System.out.println("3.修改电动车类型");
                    System.out.println("4.修改售价");
                    System.out.println("5.修改数量");
                    System.out.println("0.退出操作");
                    System.out.println("请选择你要修改的操作：");
                    int choice = sc.nextInt();
                    int line = 0;
                    switch (choice) {
                        default:
                            System.out.println("输入非法！！！");
                            break;
                        case 0:
                            System.out.println("操作已退出！！！");
                            conn.close();
                            psmt.close();
                            rs.close();
                            return;
                        case 1:
                            sql = "update electricbike set 电动车名=? where 电动车名=?";
                            psmt = conn.prepareStatement(sql);
                            System.out.println("请输入新的电动车名：");
                            sc.nextLine();//消耗掉来自nextInt的回车符
                            psmt.setString(1,sc.nextLine());
                            psmt.setString(2,alterBikeName);
                            line = psmt.executeUpdate();
                            if(line != 0) {
                                System.out.println("更新成功!!!");
                            } else {
                                System.out.println("修改失败！！！");
                            }
                            break;
                        case 2:
                            sql = "update electricbike set 产地=? where 电动车名=?";
                            psmt = conn.prepareStatement(sql);
                            System.out.println("请输入新的产地：");
                            sc.nextLine();//消耗掉来自nextInt的回车符
                            psmt.setString(1,sc.nextLine());
                            psmt.setString(2,alterBikeName);
                            line = psmt.executeUpdate();
                            if(line != 0) {
                                System.out.println("更新成功!!!");
                            }
                            break;
                        case 3:
                            sql = "update electricbike set 电动车类型=? where 电动车名=?";
                            psmt = conn.prepareStatement(sql);
                            System.out.println("请输入修改后的电动车类型：");
                            sc.nextLine();//消耗掉来自nextInt的回车符
                            psmt.setString(1,sc.nextLine());
                            psmt.setString(2,alterBikeName);
                            line = psmt.executeUpdate();
                            if(line != 0) {
                                System.out.println("更新成功!!!");
                            }
                            break;
                        case 4:
                            sql = "update electricbike set 售价=? where 电动车名=?";
                            psmt = conn.prepareStatement(sql);
                            System.out.println("请输入修改后的售价：");
                            psmt.setInt(1,sc.nextInt());
                            psmt.setString(2,alterBikeName);
                            line = psmt.executeUpdate();
                            if(line != 0) {
                                System.out.println("更新成功!!!");
                            }
                            break;
                        case 5:
                            sql = "update electricbike set 数量=? where 电动车名=?";
                            psmt = conn.prepareStatement(sql);
                            System.out.println("请输入修改后的电动车数量：");
                            psmt.setInt(1,sc.nextInt());
                            psmt.setString(2,alterBikeName);
                            line = psmt.executeUpdate();
                            if(line != 0) {
                                System.out.println("更新成功!!!");
                            }
                            break;
                    }
                }
            }

        }
        System.out.println("没有这种车，修改失败！！！");
        conn.close();
        psmt.close();
        rs.close();
    }

    @Override
    public void showBike() throws SQLException, ClassNotFoundException {
        System.out.println("展示车辆...");
        sql = "select * from electricbike";
        conn = ConnectionMS.getConnection();
        psmt = conn.prepareStatement(sql);
        rs = psmt.executeQuery();

        while (rs.next()) {
            String bikeName = rs.getString("电动车名");
            String source = rs.getString("产地");
            String type = rs.getString("电动车类型");
            int price = rs.getInt("售价");
            int count = rs.getInt("数量");
            System.out.println("EBike{" +
                    "电动车名='" + bikeName + '\'' +
                    ", 产地='" + source + '\'' +
                    ", 类型='" + type + '\'' +
                    ", 售价=" + price +
                    ", 数量=" + count +
                    '}');
        }

        rs.close();
        psmt.close();
        conn.close();
    }

    @Override
    public void exitSystem() {
        System.out.println("退出系统...");
        System.exit(1);
    }
}
